#define _CRT_SECURE_NO_WARNINGS 1
#include <utility>
#include <queue>
using namespace std;
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
using pii = pair<int, int>;
class Solution {
public:
    int bfs(int i, int j, vector<vector<int>>& grid)
    {
        queue<pii> q;
        q.push(pii(i, j));
        grid[i][j] = 0;
        int count = 1;
        while (!q.empty())
        {
            int a = q.front().first;
            int b = q.front().second;
            q.pop();
            for (int k = 0; k < 4; ++k)
            {
                int x = a + dx[k];
                int y = b + dy[k];
                if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] != 0)
                {
                    grid[x][y] = 0;
                    q.push(pii(x, y));
                    ++count;
                }
            }
        }
        return count;
    }
    int maxAreaOfIsland(vector<vector<int>>& grid)
    {
        int res = 0;
        for (int i = 0; i < grid.size(); ++i)
        {
            for (int j = 0; j < grid[0].size(); ++j)
            {
                if (grid[i][j] == 1)
                {
                    res = max(bfs(i, j, grid), res);
                }
            }
        }
        return res;
    }
};